package com.example.its2021606134chh.MyCarModule;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.its2021606134chh.HttpUtil;
import com.example.its2021606134chh.R;
import com.example.its2021606134chh.ThresholdModule.ThresholdParams;
import com.google.gson.Gson;

import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MC_remote_controlFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MC_remote_controlFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "title";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private TextView tvMyCarControl1on;
    private TextView tvMyCarControl1off;
    private TextView tvMyCarControl2on;
    private TextView tvMyCarControl2off;
    private TextView tvMyCarControl3on;
    private TextView tvMyCarControl3off;
    private TextView tvMyCarControl4on;
    private TextView tvMyCarControl4off;
    private int[] carList = {1, 2, 3, 4};   //车辆列表
    private static final int CAR1 = 1;
    private static final int CAR2 = 2;
    private static final int CAR3 = 3;
    private static final int CAR4 = 4;
    private int startColor = R.color.colorTargetNormal;
    private int stopColor = R.color.white;

    public MC_remote_controlFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MC_remote_controlFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MC_remote_controlFragment newInstance(String param1, String param2) {
        MC_remote_controlFragment fragment = new MC_remote_controlFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_m_c_remote_control, container, false);
        tvMyCarControl1on = (TextView) view.findViewById(R.id.tvMyCarControl1on);
        tvMyCarControl1off = (TextView) view.findViewById(R.id.tvMyCarControl1off);
        tvMyCarControl2on = (TextView) view.findViewById(R.id.tvMyCarControl2on);
        tvMyCarControl2off = (TextView) view.findViewById(R.id.tvMyCarControl2off);
        tvMyCarControl3on = (TextView) view.findViewById(R.id.tvMyCarControl3on);
        tvMyCarControl3off = (TextView) view.findViewById(R.id.tvMyCarControl3off);
        tvMyCarControl4on = (TextView) view.findViewById(R.id.tvMyCarControl4on);
        tvMyCarControl4off = (TextView) view.findViewById(R.id.tvMyCarControl4off);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int carId : carList) {
                    HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                        @Override
                        public void onFailure(Exception e) {

                        }

                        @SuppressLint("ResourceAsColor")
                        @Override
                        public void onResponse(String json) {
                            Gson gson = new Gson();
                            CarControl control = gson.fromJson(json, CarControl.class);
                            if (control.getRESULT().equals("S")) {
                                if (control.getStatus().equals("Stop")) {
                                    if (carId == CAR1) {
                                        Log.i("TAG", json);
                                        tvMyCarControl1off.setBackground(getResources().getDrawable(startColor));
                                        tvMyCarControl1on.setBackground(getResources().getDrawable(stopColor));
                                    } else if (carId == CAR2) {
                                        Log.i("TAG", json);
                                        tvMyCarControl2off.setBackground(getResources().getDrawable(startColor));
                                        tvMyCarControl2on.setBackground(getResources().getDrawable(stopColor));
                                    } else if (carId == CAR3) {
                                        Log.i("TAG", json);
                                        tvMyCarControl3off.setBackground(getResources().getDrawable(startColor));
                                        tvMyCarControl3on.setBackground(getResources().getDrawable(stopColor));
                                    } else if (carId == CAR4) {
                                        Log.i("TAG", json);
                                        tvMyCarControl4off.setBackground(getResources().getDrawable(startColor));
                                        tvMyCarControl4on.setBackground(getResources().getDrawable(stopColor));
                                    }
                                } else {
                                    if (carId == CAR1) {
                                        Log.i("TAG", json);
                                        tvMyCarControl1off.setBackground(getResources().getDrawable(stopColor));
                                        tvMyCarControl1on.setBackground(getResources().getDrawable(startColor));
                                    } else if (carId == CAR2) {
                                        Log.i("TAG", json);
                                        tvMyCarControl2off.setBackground(getResources().getDrawable(stopColor));
                                        tvMyCarControl2on.setBackground(getResources().getDrawable(startColor));
                                    } else if (carId == CAR3) {
                                        Log.i("TAG", json);
                                        tvMyCarControl3off.setBackground(getResources().getDrawable(stopColor));
                                        tvMyCarControl3on.setBackground(getResources().getDrawable(startColor));
                                    } else if (carId == CAR4) {
                                        Log.i("TAG", json);
                                        tvMyCarControl4off.setBackground(getResources().getDrawable(stopColor));
                                        tvMyCarControl4on.setBackground(getResources().getDrawable(startColor));
                                    }
                                }
                            } else {
                                Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
                            }
                        }
                    }, "get_car_move", new String[]{"CarId", "UserName"}, carId, "user1");
                }
            }
        }).start();

        tvMyCarControl1on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                            HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                                @Override
                                public void onFailure(Exception e) {

                                }

                                @Override
                                public void onResponse(String json) {
                                    Gson gson = new Gson();
                                    CarControl control = gson.fromJson(json, CarControl.class);
                                    if (control.getRESULT().equals("S")) {
                                        tvMyCarControl1off.setBackground(getResources().getDrawable(stopColor));
                                        tvMyCarControl1on.setBackground(getResources().getDrawable(startColor));
                                        Toast.makeText(getActivity(), "1号小车启动成功", Toast.LENGTH_SHORT).show();
                                    } else {
                                        Toast.makeText(getActivity(), "1号小车启动失败", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }, "set_car_move", new String[]{"CarId", "CarAction", "UserName"}, 1, "Start", "user1");
                    }
                }).start();
            }
        });
        tvMyCarControl1off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                            @Override
                            public void onFailure(Exception e) {

                            }

                            @Override
                            public void onResponse(String json) {
                                Gson gson = new Gson();
                                CarControl control = gson.fromJson(json, CarControl.class);
                                if (control.getRESULT().equals("S")) {
                                    tvMyCarControl1off.setBackground(getResources().getDrawable(startColor));
                                    tvMyCarControl1on.setBackground(getResources().getDrawable(stopColor));
                                    Toast.makeText(getActivity(), "1号小车停止成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "1号小车停止失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }, "set_car_move", new String[]{"CarId", "CarAction", "UserName"}, 1, "Stop", "user1");
                    }
                }).start();
            }
        });
        tvMyCarControl2on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                            @Override
                            public void onFailure(Exception e) {

                            }

                            @Override
                            public void onResponse(String json) {
                                Gson gson = new Gson();
                                CarControl control = gson.fromJson(json, CarControl.class);
                                if (control.getRESULT().equals("S")) {
                                    tvMyCarControl2off.setBackground(getResources().getDrawable(stopColor));
                                    tvMyCarControl2on.setBackground(getResources().getDrawable(startColor));
                                    Toast.makeText(getActivity(), "2号小车启动成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "2号小车启动失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }, "set_car_move", new String[]{"CarId", "CarAction", "UserName"}, 2, "Start", "user1");
                    }
                }).start();
            }
        });
        tvMyCarControl2off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                            @Override
                            public void onFailure(Exception e) {

                            }

                            @Override
                            public void onResponse(String json) {
                                Gson gson = new Gson();
                                CarControl control = gson.fromJson(json, CarControl.class);
                                if (control.getRESULT().equals("S")) {
                                    tvMyCarControl2off.setBackground(getResources().getDrawable(startColor));
                                    tvMyCarControl2on.setBackground(getResources().getDrawable(stopColor));
                                    Toast.makeText(getActivity(), "2号小车停止成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "2号小车停止失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }, "set_car_move", new String[]{"CarId", "CarAction", "UserName"}, 2, "Stop", "user1");
                    }
                }).start();
            }
        });
        tvMyCarControl3on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                            @Override
                            public void onFailure(Exception e) {

                            }

                            @Override
                            public void onResponse(String json) {
                                Gson gson = new Gson();
                                CarControl control = gson.fromJson(json, CarControl.class);
                                if (control.getRESULT().equals("S")) {
                                    tvMyCarControl3off.setBackground(getResources().getDrawable(stopColor));
                                    tvMyCarControl3on.setBackground(getResources().getDrawable(startColor));
                                    Toast.makeText(getActivity(), "3号小车启动成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "3号小车启动失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }, "set_car_move", new String[]{"CarId", "CarAction", "UserName"}, 3, "Start", "user1");
                    }
                }).start();
            }
        });
        tvMyCarControl3off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                            @Override
                            public void onFailure(Exception e) {

                            }

                            @Override
                            public void onResponse(String json) {
                                Gson gson = new Gson();
                                CarControl control = gson.fromJson(json, CarControl.class);
                                if (control.getRESULT().equals("S")) {
                                    tvMyCarControl3off.setBackground(getResources().getDrawable(startColor));
                                    tvMyCarControl3on.setBackground(getResources().getDrawable(stopColor));
                                    Toast.makeText(getActivity(), "3号小车停止成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "3号小车停止失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }, "set_car_move", new String[]{"CarId", "CarAction", "UserName"}, 3, "Stop", "user1");
                    }
                }).start();
            }
        });
        tvMyCarControl4on.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                            @Override
                            public void onFailure(Exception e) {

                            }

                            @Override
                            public void onResponse(String json) {
                                Gson gson = new Gson();
                                CarControl control = gson.fromJson(json, CarControl.class);
                                if (control.getRESULT().equals("S")) {
                                    tvMyCarControl4off.setBackground(getResources().getDrawable(stopColor));
                                    tvMyCarControl4on.setBackground(getResources().getDrawable(startColor));
                                    Toast.makeText(getActivity(), "4号小车启动成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "4号小车启动失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }, "set_car_move", new String[]{"CarId", "CarAction", "UserName"}, 4, "Start", "user1");
                    }
                }).start();
            }
        });
        tvMyCarControl4off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        HttpUtil.asyncRequest(new HttpUtil.AsyncCallBack() {
                            @Override
                            public void onFailure(Exception e) {

                            }

                            @Override
                            public void onResponse(String json) {
                                Gson gson = new Gson();
                                CarControl control = gson.fromJson(json, CarControl.class);
                                if (control.getRESULT().equals("S")) {
                                    tvMyCarControl4off.setBackground(getResources().getDrawable(startColor));
                                    tvMyCarControl4on.setBackground(getResources().getDrawable(stopColor));
                                    Toast.makeText(getActivity(), "4号小车停止成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getActivity(), "3号小车停止失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        }, "set_car_move", new String[]{"CarId", "CarAction", "UserName"}, 4, "Stop", "user1");
                    }
                }).start();
            }
        });

        return view;
    }

}